//
//  AYSCategoryViewController.m
//  AiYingShiProject
//
//  Created by lanou on 15/10/27.
//  Copyright (c) 2015年 李晓理. All rights reserved.
//

#import "AYSCategoryViewController.h"
#import "AYSCategoryView.h"
#import "AYSCategoryDetailViewController.h"
#import "AYSHomeMoreDataViewController.h"
#import "AYSRequestManager.h"
#import "AYSCategoryMainModel.h"
#import <UIImageView+WebCache.h>
#import "AYSCategorySmallViewController.h"
#import "AYSSideViewController.h"
#import "AppDelegate.h"
#define Start_X 37.0f           // 第一个按钮的X坐标
//#define Start_Y _categoryView.zyButton.frame.size.height * 3 + 50         // 第一个按钮的Y坐标
#define Start_Y 350
#define Width_Space 45.0f        // 2个按钮之间的横间距
#define Height_Space 20.0f      // 竖间距
#define Button_Height 70.0f    // 高
#define Button_Width 70.0f      // 宽
@interface AYSCategoryViewController ()
@property (nonatomic, strong) AYSCategoryDetailViewController *categoryDetailVC;
@property (nonatomic, strong) AYSHomeMoreDataViewController *moreVC;
@property(nonatomic, strong)NSMutableArray *smallMainArr;
@end

@implementation AYSCategoryViewController
- (NSMutableArray *)smallMainArr
{
    if (_smallMainArr == nil)
    {
        self.smallMainArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _smallMainArr;
}

- (void)requestDataWithCategory
{
   [AYSRequestManager requestWithType:GET url:kClassificationUrl parDic:nil finish:^(NSData *data){
       NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
            //获取数组中的键值对
            for (NSDictionary *dic in array) {
                AYSCategoryMainModel *model = [[AYSCategoryMainModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.smallMainArr addObject:model];
               
               
            }
       CGRect rect = ((UILabel *)self.view.subviews[4]).frame;
       UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(rect), SCREEN_WIDTH, SCREEN_HEIGHT - CGRectGetMaxY(rect) - 49)];
       tempView.backgroundColor = [UIColor clearColor];
       [self.view addSubview:tempView];
       CGFloat smallRadius = (tempView.frame.size.height - 20) / 3;
       CGFloat placeholdWidth = (tempView.frame.size.width - smallRadius * 3) / 4;
       // 循环创建button
       for (int i = 0; i < 9; i++)
       {
           NSInteger index = i % 3;
           NSInteger page = i / 3;
           
           UIButton *smallButton = [UIButton buttonWithType:UIButtonTypeCustom];
           smallButton.imageView.layer.cornerRadius = smallRadius / 2;
           smallButton.tag = i + 100;
           smallButton.imageView.layer.masksToBounds = YES;
           [smallButton addTarget:self action:@selector(didClickSmallButton:) forControlEvents:(UIControlEventTouchUpInside)];
           smallButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
           smallButton.backgroundColor = [UIColor clearColor];
//           smallButton.frame = CGRectMake(index * (Button_Width + Width_Space) + Start_X, page  * (Button_Height + Height_Space)+Start_Y, Button_Width, Button_Height);
           
           smallButton.frame = CGRectMake(index * (placeholdWidth + smallRadius) + placeholdWidth, page * (smallRadius + 10), smallRadius, smallRadius);
           
           AYSCategoryMainModel *model = _smallMainArr[i];
           [smallButton setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d", i]] forState:(UIControlStateNormal)];
            [tempView addSubview:smallButton];
           
           UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, smallButton.frame.size.width, smallButton.frame.size.height)];
           label.text = model.name;
           label.textColor = [UIColor whiteColor];
           label.textAlignment = NSTextAlignmentCenter;
           [smallButton addSubview:label];
       }
       } error:^(NSError *error)
         {
             NSLog(@"更多电影网络请求失败");
         }];
        
}


- (void)didClickSmallButton:(UIButton *)sender
{
    
    NSInteger i = sender.tag - 100;
    AYSCategoryMainModel *model = _smallMainArr[i];
    AYSCategorySmallViewController *smallVC = [[AYSCategorySmallViewController alloc] init];
    smallVC.classNameStr = model.name;
    [self.navigationController pushViewController:smallVC animated:YES];
}



- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor blackColor];
    self.navigationItem.title = @"分类";
    UIColor *color = [UIColor whiteColor];
    NSDictionary *dict = [NSDictionary dictionaryWithObject:color forKey:UITextAttributeTextColor];
    self.navigationController.navigationBar.titleTextAttributes = dict;
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithWhite:0.032 alpha:1.000];

    
    UIImageView *backView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen]bounds]];
    backView.image = [UIImage imageNamed:@"4"];
    [self.view addSubview:backView];
    
   
      UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"抽屉菜单"] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)] style:(UIBarButtonItemStylePlain) target:self action:@selector(clickLeftButton)];
    self.navigationItem.leftBarButtonItem = leftButton;
   
    AYSCategoryView *categoryView = [[AYSCategoryView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, 300)];
    
       self.view = categoryView;
    self.view = categoryView;
    categoryView.movieButtonBlock = ^void(void)
    {
        _categoryDetailVC = [[AYSCategoryDetailViewController alloc] init];
        _categoryDetailVC.type = CategoryTypeMovie;
        [self.navigationController pushViewController:_categoryDetailVC animated:YES];
    };
    categoryView.tvButtonBlock = ^void(void)
    {
        _categoryDetailVC = [[AYSCategoryDetailViewController alloc] init];
        _categoryDetailVC.type = CategoryTypeTV;
        [self.navigationController pushViewController:_categoryDetailVC animated:YES];
    };
    categoryView.cartonButtonBlock = ^void(void)
    {
        _categoryDetailVC = [[AYSCategoryDetailViewController alloc] init];
        _categoryDetailVC.type = CategoryTypeCarton;
        [self.navigationController pushViewController:_categoryDetailVC animated:YES];
    };
    categoryView.zyButtonBlock = ^void(void)
    {
        _moreVC = [[AYSHomeMoreDataViewController alloc] init];
        _moreVC.navigationController = self.navigationController;
        _moreVC.type = CategoryTypeZY;
        [self.navigationController pushViewController:_moreVC animated:YES];
    };
    
    [self requestDataWithCategory];

}

- (void)clickLeftButton
{
    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
    AYSSideViewController *sideViewController = [delegate sideViewController];
    [sideViewController showLeftViewController:true];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
